package tt.dz.appserver.mvc.controller;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.business.ReservationService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;


/**
 * 预约业务控制器 多例
 * @author xielf
 *
 */
@RestController
@RequestMapping("/reservation")
@Scope(value="prototype")
public class ReservationController {

	
	/**预约业务接口*/
	@Resource
	private ReservationService reservationService;
	

	/**
	 * 预约
	 * @param userCard
	 * @param chargerId
	 * @return
	 */
	@RequestMapping(value="/reservation",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	public String reservation(String userCard, String chargerId, String plugNo){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			JSONObject omsServerResult = reservationService.reservation(userCard, chargerId, plugNo);
			String code = omsServerResult.getString("code");
			String msg = omsServerResult.getString("msg");
			
			if(Response.SUCCESS.getCode().equals(code)){

				omsServerResult.remove("code");
				omsServerResult.remove("msg");
				//返回值封装
				resultMappingEntity = Result.responseBody(code, msg, omsServerResult);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 取消预约
	 * @param reservationSn
	 * @return
	 */
	@RequestMapping(value="/cancelReservation",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public String cancelReservation(String reservationSn){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			JSONObject omsServerResult = reservationService.cancelReservation(reservationSn);
			String code = omsServerResult.getString("code");
			String msg = omsServerResult.getString("msg");
			
			if(Response.SUCCESS.getCode().equals(code)){
				
				omsServerResult.remove("code");
				omsServerResult.remove("msg");
				//返回值封装
				resultMappingEntity = Result.responseBody(code, msg, omsServerResult);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 获取预约列表
	 * @param userCard
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value="/reservationList",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String reservationList(String userCard, Integer pageNo){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			JSONObject omsServerResult = reservationService.reservationList(userCard, pageNo);
			String code = omsServerResult.getString("code");
			String msg = omsServerResult.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){

				omsServerResult.remove("code");
				omsServerResult.remove("msg");
				//返回值封装
				resultMappingEntity = Result.responseBody(code, msg, omsServerResult);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 预约记录详细
	 * @param reservationSn
	 * @return
	 */
	@RequestMapping(value="/reservationDetail",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String reservationDetail(String reservationSn){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			JSONObject omsServerResult = reservationService.getReservationDetail(reservationSn);
			String code = omsServerResult.getString("code");
			String msg = omsServerResult.getString("msg");

			if(Response.SUCCESS.getCode().equals(code)){

				omsServerResult.remove("code");
				omsServerResult.remove("msg");
				//返回值封装
				resultMappingEntity = Result.responseBody(code, msg, omsServerResult.get("reservationDetail"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
}
